package javagprssrv.StructClass;

import java.nio.ByteBuffer;
import java.net.InetAddress;
import java.net.DatagramPacket;
import javagprssrv.Common.CommonClass;
import java.net.SocketAddress;
import javagprssrv.JGS_Param;
import java.net.InetSocketAddress;
import java.net.DatagramSocket;
import java.nio.channels.SocketChannel;

public class structOutputSocket
{
    public java.lang.String Protocol;
    public java.lang.String RemoteServerIP;
    public int RemoteServerPort;
    public java.lang.String SenderProtocol;
    public java.lang.String SenderIP;
    public int SenderPort;
    private boolean canBeTCPConnected;
    private java.nio.channels.SocketChannel TCPclient;
    private java.net.DatagramSocket UDPclient;
    
    public structOutputSocket(final java.lang.String _Protocol, final java.lang.String _RemoteServerIP, final int _RemoteServerPort) {
        this.Protocol = "";
        this.RemoteServerIP = "";
        this.RemoteServerPort = -1;
        this.SenderProtocol = "";
        this.SenderIP = "";
        this.SenderPort = -1;
        this.canBeTCPConnected = true;
        this.Protocol = _Protocol.trim().toUpperCase();
        this.RemoteServerIP = _RemoteServerIP;
        this.RemoteServerPort = _RemoteServerPort;
        this.init();
    }
    
    private void init() {
        if (this.Protocol.equals("TCP")) {
            try {
                final java.net.SocketAddress address = new java.net.InetSocketAddress(this.RemoteServerIP, this.RemoteServerPort);
                this.TCPclient = java.nio.channels.SocketChannel.open(address);
                javagprssrv.JGS_Param.myLog.ShowLog("输出端口 ( [" + this.Protocol + "] " + this.RemoteServerIP + ":" + java.lang.String.valueOf(this.RemoteServerPort) + " ) 打开成功。", true, true);
            }
            catch (java.lang.Exception e) {
                javagprssrv.JGS_Param.myLog.ShowLog("输出端口 ( [" + this.Protocol + "] " + this.RemoteServerIP + ":" + java.lang.String.valueOf(this.RemoteServerPort) + " ) 打开 <<<<<  失败  >>>>> ！！！", true, true);
                this.canBeTCPConnected = false;
            }
        }
        if (this.Protocol.equals("UDP")) {
            try {
                this.UDPclient = new java.net.DatagramSocket();
                javagprssrv.JGS_Param.myLog.ShowLog("输出端口 ( [" + this.Protocol + "] " + this.RemoteServerIP + ":" + java.lang.String.valueOf(this.RemoteServerPort) + " ) 打开成功。", true, true);
            }
            catch (java.lang.Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public void SendData(final java.lang.String strSend) {
        if (this.Protocol.equals("TCP")) {
            if (!this.canBeTCPConnected) {
                return;
            }
            final java.nio.ByteBuffer buffer = javagprssrv.Common.CommonClass.String2ByteBuffer(strSend);
            try {
                this.TCPclient.write(buffer);
            }
            catch (java.lang.Exception e) {
                e.printStackTrace();
                this.canBeTCPConnected = false;
            }
        }
        if (this.Protocol.equals("UDP")) {
            try {
                final java.net.DatagramPacket DP = new java.net.DatagramPacket(strSend.getBytes(), strSend.getBytes().length);
                DP.setAddress(java.net.InetAddress.getByName(this.RemoteServerIP));
                DP.setPort(this.RemoteServerPort);
                byte[] buf = new byte[strSend.length()];
                buf = javagprssrv.Common.CommonClass.String2ByteArray(strSend);
                DP.setData(buf, 0, strSend.length());
                this.UDPclient.send(DP);
            }
            catch (java.lang.Exception e2) {
                e2.printStackTrace();
            }
        }
    }
}
